package com.shaozi.nc.aop;

import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import com.shaozi.lib.execpool.RunnableExecutor;
import com.shaozi.lib.execpool.base.ExecutorDataBean;
import com.shaozi.nc.bean.resp.RespMessageSetReadForTypeBean;
import com.shaozi.nc.data.bean.CompanyUserBean;

public abstract class BaseManageAop {
	@Autowired
	private RunnableExecutor runExecutor;

	protected void baseAfterExecute(JoinPoint jp, boolean retVal, Class runnableClz) {
		if (!retVal)
			return;
		RespMessageSetReadForTypeBean respBean = findRespArg(jp.getArgs());
		if (respBean == null)
			return;
		CompanyUserBean companyUserBean = findReqArg(jp.getArgs());
		if (companyUserBean == null)
			return;

		ExecutorDataBean logBean = new ExecutorDataBean();
		logBean.runnableClz = runnableClz;
		logBean.datas.put("respBean", respBean);
		logBean.datas.put("companyUserBean", companyUserBean);
		runExecutor.execute(logBean);
	}

	protected void baseAfterExecuteObserver(JoinPoint jp, boolean retVal, Class runnableClz) {
		if (!retVal)
			return;
		RespMessageSetReadForTypeBean respBean = findRespArg(jp.getArgs());
		if (respBean == null)
			return;
		CompanyUserBean companyUserBean = findReqArg(jp.getArgs());
		if (companyUserBean == null)
			return;

		ExecutorDataBean logBean = new ExecutorDataBean();
		logBean.runnableClz = runnableClz;
		logBean.datas.put("respBean", respBean);
		logBean.datas.put("companyUserBean", companyUserBean);
		runExecutor.execute(logBean);
	}

	private CompanyUserBean findReqArg(Object[] args) {
		for (Object arg : args) {
			if (arg == null)
				continue;
			if (arg instanceof CompanyUserBean)
				return (CompanyUserBean) arg;
		}
		return null;
	}

	private RespMessageSetReadForTypeBean findRespArg(Object[] args) {
		for (Object arg : args) {
			if (arg == null)
				continue;
			if (arg instanceof RespMessageSetReadForTypeBean)
				return (RespMessageSetReadForTypeBean) arg;
		}
		return null;
	}
}
